//
//  ChangeTextFont.swift
//  anxinapp
//
//  Created by iDev on 6/23/17.
//  Copyright © 2017 iDev. All rights reserved.
//

import UIKit

func FontColorText(String str:String, color:UIColor, cstart:Int,clen:Int,font:UIFont, fstart:Int, flen:Int)->NSMutableAttributedString{
    var mutableStr = NSMutableAttributedString(string: str)
    mutableStr = getFontColorText(String: mutableStr, color: color, cstart: cstart, clen: clen, font: font, fstart: fstart, flen: flen)
    return mutableStr
}

func FontColorText(String str:String, color:UIColor, font:UIFont, start:Int, len:Int)->NSMutableAttributedString{
    var mutableStr = NSMutableAttributedString(string: str)
    mutableStr = getFontColorText(String: mutableStr, color: color, cstart: start, clen: len, font: font, fstart: start, flen: len)
    return mutableStr
}

func FontText(String str:String, font:UIFont, start:Int, len:Int)->NSMutableAttributedString{
    var mutableStr = NSMutableAttributedString(string: str)
    mutableStr = getFontText(String: mutableStr, font: font, start: start, len: len)
    return mutableStr
}

func ColorText(String str:String, color:UIColor,  start:Int, len:Int)->NSMutableAttributedString{
    var mutableStr = NSMutableAttributedString(string: str)
    mutableStr = getColorText(String: mutableStr, color: color, start: start, len: len)
    return mutableStr
}

private func getFontText(String str:NSMutableAttributedString, font:UIFont,  start:Int, len:Int)->NSMutableAttributedString{
    let mutableStr = str
    let rangeStart = start>0 ? start : 0
    let rangeLen = str.length-start>len ? len : str.length-start
    
    mutableStr.addAttribute(NSFontAttributeName, value: font, range: NSMakeRange(rangeStart, rangeLen))
    return mutableStr
}

private func getColorText(String str:NSMutableAttributedString, color:UIColor,  start:Int, len:Int)->NSMutableAttributedString{
    let mutableStr = str
    let rangeStart = start>0 ? start : 0
    let rangeLen = str.length-start>len ? len : str.length-start
    
    mutableStr.addAttribute(NSForegroundColorAttributeName, value: color, range: NSMakeRange(rangeStart, rangeLen))
    return mutableStr
}

private func getFontColorText(String str:NSMutableAttributedString, color:UIColor,  cstart:Int, clen:Int,
                      font:UIFont, fstart:Int, flen:Int)->NSMutableAttributedString{
    var mutableStr = str
    mutableStr = getColorText(String: str, color: color, start: cstart, len: clen)
    mutableStr = getFontText(String: mutableStr, font: font, start: fstart, len: flen)
    return mutableStr
}
